{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "divine-fishing",
   "metadata": {},
   "source": [
    "# Local Interpretable Model-agnostic Explanations\n",
    "\n",
    "Link to API Reference: [LimeTabular](./python/api/LimeTabular.ipynb)\n",
    "\n",
    "*See the backing repository for LIME [here](https://github.com/marcotcr/lime).*\n",
    "\n",
    "<h2>Summary</h2>\n",
    "\n",
    "Local interpretable model-agnostic explanations (LIME)[[1](ribeiro2016should_lime)] is a method that fits a surrogate glassbox model around the decision space of any blackbox model's prediction. LIME explicitly tries to model the local neighborhood of any prediction -- by focusing on a narrow enough decision surface, even simple linear models can provide good approximations of blackbox model behavior. Users can then inspect the glassbox model to understand how the blackbox model behaves in that region. \n",
    "\n",
    "LIME works by perturbing any individual datapoint and generating synthetic data which gets evaluated by the blackbox system, and ultimately used as a training set for the glassbox model. LIME's advantages are that you can interpret an explanation the same way you reason about a linear model, and that it can be used on almost any model. On the otherhand, explanations are occasionally unstable and highly dependent on the perturbation process.\n",
    "\n",
    "<h2>How it Works</h2>\n",
    "\n",
    "Christoph Molnar's \"Interpretable Machine Learning\" e-book [[2](molnar2020interpretable_lime)] has an excellent overview on LIME that can be found [here](https://christophm.github.io/interpretable-ml-book/lime.html).\n",
    "\n",
    "The conceiving paper \"Why should i trust you?\" Explaining the predictions of any classifier.\" [[1](ribeiro2016should_lime)] can be found on arXiv [here](https://arxiv.org/pdf/1602.04938.pdf).\n",
    "\n",
    "If you find video as a better medium for learning the algorithm, you can find a conceptual overview of the algorithm by the author Marco Tulio Ribeiro below:\n",
    "[![The Science Behind InterpretML: LIME](https://img.youtube.com/vi/g2WtL45-PFQ/0.jpg)](https://www.youtube.com/watch?v=g2WtL45-PFQ \"The Science Behind InterpretML: LIME\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "meaningful-default",
   "metadata": {},
   "source": [
    "<h2>Code Example</h2>\n",
    "\n",
    "The following code will train a blackbox pipeline for the breast cancer dataset. Aftewards it will interpret the pipeline and its decisions with LIME. The visualizations provided will be for local explanations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "recreational-carrier",
   "metadata": {},
   "outputs": [],
   "source": [
    "from interpret import set_visualize_provider\n",
    "from interpret.provider import InlineProvider\n",
    "set_visualize_provider(InlineProvider())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bronze-peripheral",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.datasets import load_breast_cancer\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.pipeline import Pipeline\n",
    "\n",
    "from interpret import show\n",
    "from interpret.blackbox import LimeTabular\n",
    "\n",
    "seed = 42\n",
    "np.random.seed(seed)\n",
    "X, y = load_breast_cancer(return_X_y=True, as_frame=True)\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=seed)\n",
    "\n",
    "pca = PCA()\n",
    "rf = RandomForestClassifier(random_state=seed)\n",
    "\n",
    "blackbox_model = Pipeline([('pca', pca), ('rf', rf)])\n",
    "blackbox_model.fit(X_train, y_train)\n",
    "\n",
    "lime = LimeTabular(blackbox_model, X_train)\n",
    "\n",
    "show(lime.explain_local(X_test[:5], y_test[:5]), 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "legal-blackberry",
   "metadata": {},
   "source": [
    "<h2>Further Resources</h2>\n",
    "\n",
    "- [LIME GitHub Repository](https://github.com/marcotcr/lime)\n",
    "- [LIME Video for KDD2016](https://www.youtube.com/watch?v=hUnRCxnydCc)\n",
    "- [Conceptual Video for LIME](https://www.youtube.com/watch?v=g2WtL45-PFQ)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "novel-climb",
   "metadata": {},
   "source": [
    "<h2>Bibliography</h2>\n",
    "\n",
    "(ribeiro2016should_lime)=\n",
    "[1] Marco Tulio Ribeiro, Sameer Singh, and Carlos Guestrin. \" why should i trust you?\" explaining the predictions of any classifier. In Proceedings of the 22nd ACM SIGKDD international conference on knowledge discovery and data mining, 1135–1144. 2016.\n",
    "\n",
    "(molnar2020interpretable_lime)=\n",
    "[2] Christoph Molnar. Interpretable machine learning. Lulu. com, 2020."
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
